package cn.edu.nuc.ssm.service.impl;

import cn.edu.nuc.ssm.dao.AccountMapper;
import cn.edu.nuc.ssm.dto.EverySumCaiWuDto;
import cn.edu.nuc.ssm.dto.SumCaiWuDto;
import cn.edu.nuc.ssm.entity.Account;
import cn.edu.nuc.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/4/8.
 */
@Service
public class AccountServiceImpl implements AccountService{

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private AccountMapper accountMapper;


    public Map<String,List<Account>> listUserAcount(int uid){

        Map<String,List<Account>> map = new HashMap<>();
        List<String> typelist = accountMapper.findaccounttype();
        List<Account> accountList = accountMapper.findbyUid(uid);


        for (String type:typelist){
            map.put(type,new ArrayList<Account>());
            for (Account account:accountList){
                if (type.equals(account.getAccounttype())){

                    map.get(type).add(account);
                }
            }
        }
        return map;
    }
    @Override
    public SumCaiWuDto sumCaiWu(int uid) {

        float zhichan = accountMapper.sumZhichan(uid);
        float sumFuzai = accountMapper.sumFuzai(uid);

        return new SumCaiWuDto(zhichan,sumFuzai);
    }



    public  List<EverySumCaiWuDto> sumEverySumCaiWuDto(int uid){

        List<EverySumCaiWuDto> everySumCaiWuDtos = accountMapper.everyAccontTypeSum(uid);

        return everySumCaiWuDtos;

    }
    public Account findbyId(int id){

        return accountMapper.findbyId(id);
    }
    @Transactional(timeout = 30)
    @Override
    public int initUserAccount(int uid) {

        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormatter.format(new Date());

        try {
            Account account1 = new Account(uid,"现金",new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),dateStr,"现金");
            accountMapper.insert(account1);

            Account account2 = new Account(uid,"银行卡",new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),dateStr,"银行卡");
            accountMapper.insert(account2);

            Account account3 = new Account(uid,"应付款项",new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),dateStr,"负债账户");
            accountMapper.insert(account3);

            Account account4 = new Account(uid,"应收款项",new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),dateStr,"债权账户");
            accountMapper.insert(account4);

            Account account5 = new Account(uid,"余额包",new BigDecimal(0),new BigDecimal(0),new BigDecimal(0),dateStr,"余额包");
            accountMapper.insert(account5);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    @Override
    public void updateYuE(Account account) {

        accountMapper.updateRemainMoneybyId(account.getRemainMoney(),account.getId());



    }
}
